package com.atguigu.spzx.manager.config;

import com.atguigu.spzx.manager.Interceptor.LoginAuthInterceptor;
import com.atguigu.spzx.manager.properties.SpzxAuthProperties;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * @author syh
 * @create 2023-08-12 19:18
 */
@Component
@EnableConfigurationProperties(SpzxAuthProperties.class)
public class WebMvcConfiguration implements WebMvcConfigurer {
    @Autowired
    private LoginAuthInterceptor loginAuthInterceptor ;

    @Resource
    SpzxAuthProperties spzxAuthProperties;
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")// 添加路径规则
                .allowCredentials(true) // 是否允许在跨域的情况下传递Cookie
                .allowedOriginPatterns("*") // 允许请求来源的域规则
                .allowedMethods("*")
                .allowedHeaders("*") ;  // 允许所有的请求头
    }
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(loginAuthInterceptor)
                .excludePathPatterns(spzxAuthProperties.getPaths())
                .addPathPatterns("/**");
    }

}
